package util

import (
	"strconv"

	"github.com/gin-gonic/gin"
)

type Page struct {
    Page     int
    PageSize int
    Offset   int
    Limit    int
}

func GetPage(c *gin.Context) Page {
    const (
        defaultPage     = 1
        defaultPageSize = 10
        maxPageSize     = 100
    )
    p, _ := strconv.Atoi(c.DefaultQuery("page", strconv.Itoa(defaultPage)))
    ps, _ := strconv.Atoi(c.DefaultQuery("pageSize", strconv.Itoa(defaultPageSize)))
    if p < 1 { p = defaultPage }
    if ps < 1 { ps = defaultPageSize }
    if ps > maxPageSize { ps = maxPageSize }
    return Page{Page: p, PageSize: ps, Offset: (p-1)*ps, Limit: ps}
}
